package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 成都全搜索网解析
 * @author Persh
 *
 */
public class ChengDuNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.chengdu.cn/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/[^.]*.htm[?]node=[0-9]*",
		"http://.*.chengdu.cn/[0-9]*/[0-9]*/[0-9]{5,}.shtml"
		};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
			title =doc.select("h1#art_title.tit").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div.content>div.colL_T>div.tit>h2").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("div.content>h1.tit>a.bt_link").text();
					if(title==null || title.trim().length()==0){
					}
				}
			}
			content=HtmlCleaner.getContentHtml(url ,doc.select("div.article-content"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.contxt"));
			}
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.content>div.colR>div.info>p#intros"));
			}
			if(content!=null){
				content=content.replace("正在加载中... ", "");
			}
			date=DateUtils.matchDate(doc.select("div.content>h2.font_gray").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.content>div.colL_T>div.tit>p").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.content>h2.font_gray").text());
			}
			if(date==null){
				date=DateUtils.matchDate(doc.select("h2.font_gray").text());
			}


			String commentStr=doc.select("div.titleBar>span.f14px").text();
			Integer commentNum=0;
			try {
				commentNum=Integer.parseInt(commentStr.substring(commentStr.indexOf("有")+1, commentStr.indexOf("条")));
			} catch (Exception e) {
			}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
			news.setCommentNum(commentNum);

		return news;
	}

	public static void main(String[] args) {
		ChengDuNewsAnalyse analyse=new ChengDuNewsAnalyse();
		String url="http://news.chengdu.cn/2016/1108/1830175.shtml";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
